﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TjZheng.News.Business;
using TjZheng.News.DataEntity;
using TjZheng.Cache;
using TjZheng.Framework;
using System.Text.RegularExpressions;

namespace TjZheng.News.Process
{
    public static class ArticleHelper
    {

        public static ArticleResultInfo CreateArticleResultInfo(this ArticleInfo objArticleInfo, bool isLoadArticleContent, bool isLoadPictureCollection = false)
        {
            if (objArticleInfo == null)
            {
                return null;
            }
            ArticleResultInfo objArticleResultInfo = new ArticleResultInfo();
            objArticleResultInfo.ArticleInfo = objArticleInfo;
            BizArticleInfoData objBizArticleInfoData = new BizArticleInfoData(objArticleInfo.CategoryId.ToString());
            List<ArticleInfoData> objArticleInfoDataList = objBizArticleInfoData.GetListCache("", TimeSpan.FromMinutes(1), "ArticleId=" + objArticleInfo.ArticleId);
            ArticleInfoData objArticleInfoData = null;
            if (objArticleInfoDataList.IsNoNull())
            {
                objArticleInfoData = objArticleInfoDataList.FirstOrDefault(c => c.InfoDataType == ArticleInfoDataType.ArticleContent);
                if (objArticleInfoData != null)
                {
                    objArticleResultInfo.ArticleContent = objArticleInfoData.ArticleContent;
                }
                objArticleInfoData = objArticleInfoDataList.FirstOrDefault(c => c.InfoDataType == ArticleInfoDataType.PictureCollection);
                if (objArticleInfoData != null)
                {
                    objArticleResultInfo.PictureCollection = objArticleInfoData.ArticleContent.JsonDeserialize<List<ArticlePicture>>();
                }
            }
            return objArticleResultInfo;
        }

        public static List<ArticleResultInfo> CreateArticleResultInfoList(this List<ArticleInfo> objArticleInfoList, bool isLoadArticleContent, bool isLoadPictureCollection = false)
        {
            List<ArticleResultInfo> objArticleResultInfoList = new List<ArticleResultInfo>();
            if (objArticleInfoList.IsNull())
            {
                return objArticleResultInfoList;
            }
            ArticleResultInfo objArticleResultInfo = null;
            if (!isLoadArticleContent && !isLoadPictureCollection)
            {
                foreach (ArticleInfo objArticleInfo in objArticleInfoList)
                {
                    objArticleResultInfo = new ArticleResultInfo { ArticleInfo = objArticleInfo };
                    objArticleResultInfoList.Add(objArticleResultInfo);
                }
                return objArticleResultInfoList;
            }

            List<ArticleInfoData> objList = null;
            ArticleInfoData objArticleInfoData = null;
            BizArticleInfoData objBizArticleInfoData = new BizArticleInfoData(objArticleInfoList.First().CategoryId.ToString());
            if (isLoadArticleContent && isLoadPictureCollection)
            {
                objList = objBizArticleInfoData.GetListCache("", TimeSpan.FromMinutes(1), string.Format("ArticleId in({0})", objArticleInfoList.Select(c => c.ArticleId).JoinComma()));
                if (objList.IsNoNull())
                {
                    List<ArticleInfoData> objArticleInfoDataList = null;
                    foreach (ArticleInfo objArticleInfo in objArticleInfoList)
                    {
                        objArticleResultInfo = new ArticleResultInfo
                        {
                            ArticleInfo = objArticleInfo
                        };
                        objArticleInfoDataList = objList.Where(c => c.ArticleId == objArticleInfo.ArticleId).ToList();
                        if (objArticleInfoDataList.IsNoNull())
                        {
                            objArticleInfoData = objArticleInfoDataList.FirstOrDefault(c => c.InfoDataType == ArticleInfoDataType.ArticleContent);
                            if (objArticleInfoData != null)
                            {
                                objArticleResultInfo.ArticleContent = objArticleInfoData.ArticleContent;
                            }
                            objArticleInfoData = objArticleInfoDataList.FirstOrDefault(c => c.InfoDataType == ArticleInfoDataType.PictureCollection);
                            if (objArticleInfoData != null)
                            {
                                objArticleResultInfo.PictureCollection = objArticleInfoData.ArticleContent.JsonDeserialize<List<ArticlePicture>>();
                            }
                        }
                        objArticleResultInfoList.Add(objArticleResultInfo);
                    }
                }
                return objArticleResultInfoList;
            }

            if (isLoadArticleContent)
            {
                objList = objBizArticleInfoData.GetListCache("", TimeSpan.FromMinutes(1), string.Format("ArticleId in({0}) AND InfoDataType=0", objArticleResultInfoList.Select(c => c.ArticleInfo.ArticleId).JoinComma()));
                if (objList.IsNoNull())
                {
                    foreach (ArticleInfo objArticleInfo in objArticleInfoList)
                    {
                        objArticleResultInfo = new ArticleResultInfo
                        {
                            ArticleInfo = objArticleInfo
                        };
                        objArticleInfoData = objList.FirstOrDefault(c => c.ArticleId == objArticleInfo.ArticleId);
                        if (objArticleInfoData != null)
                        {
                            objArticleResultInfo.ArticleContent = objArticleInfoData.ArticleContent;
                        }
                        objArticleResultInfoList.Add(objArticleResultInfo);
                    }
                }
            }
            if (isLoadPictureCollection)
            {
                objList = objBizArticleInfoData.GetListCache("", TimeSpan.FromMinutes(1), string.Format("ArticleId in({0}) AND InfoDataType=1", objArticleResultInfoList.Select(c => c.ArticleInfo.ArticleId).JoinComma()));
                if (objList.IsNoNull())
                {
                    foreach (ArticleInfo objArticleInfo in objArticleInfoList)
                    {
                        objArticleInfoData = objList.FirstOrDefault(c => c.ArticleId == objArticleResultInfo.ArticleInfo.ArticleId);
                        objArticleResultInfo = objArticleResultInfoList.FirstOrDefault(c => c.ArticleInfo.ArticleId == objArticleInfo.ArticleId);
                        if (objArticleResultInfo != null)
                        {
                            if (objArticleInfoData != null)
                            {
                                objArticleResultInfo.PictureCollection = objArticleInfoData.ArticleContent.JsonDeserialize<List<ArticlePicture>>();
                            }
                            continue;
                        }
                        objArticleResultInfo = new ArticleResultInfo
                        {
                            ArticleInfo = objArticleInfo
                        };
                        if (objArticleInfoData != null)
                        {
                            objArticleResultInfo.PictureCollection = objArticleInfoData.ArticleContent.JsonDeserialize<List<ArticlePicture>>();
                        }
                        objArticleResultInfoList.Add(objArticleResultInfo);
                    }
                }
            }
            return objArticleResultInfoList;
        }

        public static ArticleResultInfo CreateArticleResultInfo(this ArticleCollect objArticleCollect, bool isLoadArticleContent, bool isLoadPictureCollection = false)
        {
            if (objArticleCollect == null)
            {
                return null;
            }
            ArticleResultInfo objArticleResultInfo = new ArticleResultInfo();
            objArticleResultInfo.ArticleCollect = objArticleCollect;
            BizArticleInfoData objBizArticleInfoData = new BizArticleInfoData(objArticleCollect.CategoryId.ToString());
            List<ArticleInfoData> objArticleInfoDataList = objBizArticleInfoData.GetListCache("", TimeSpan.FromMinutes(1), "ArticleId=" + objArticleCollect.ArticleId);
            ArticleInfoData objArticleInfoData = null;
            if (objArticleInfoDataList.IsNoNull())
            {
                objArticleInfoData = objArticleInfoDataList.FirstOrDefault(c => c.InfoDataType == ArticleInfoDataType.ArticleContent);
                if (objArticleInfoData != null)
                {
                    objArticleResultInfo.ArticleContent = objArticleInfoData.ArticleContent;
                }
                objArticleInfoData = objArticleInfoDataList.FirstOrDefault(c => c.InfoDataType == ArticleInfoDataType.PictureCollection);
                if (objArticleInfoData != null)
                {
                    objArticleResultInfo.PictureCollection = objArticleInfoData.ArticleContent.JsonDeserialize<List<ArticlePicture>>();
                }
            }
            return objArticleResultInfo;
        }

        public static List<ArticleResultInfo> CreateArticleResultInfoList(this List<ArticleCollect> objArticleCollectList, bool isLoadArticleContent, bool isLoadPictureCollection = false)
        {
            List<ArticleResultInfo> objArticleResultInfoList = new List<ArticleResultInfo>();
            if (objArticleCollectList.IsNull())
            {
                return objArticleResultInfoList;
            }
            ArticleResultInfo objArticleResultInfo = null;
            if (!isLoadArticleContent && !isLoadPictureCollection)
            {
                foreach (ArticleCollect objArticleCollect in objArticleCollectList)
                {
                    objArticleResultInfo = new ArticleResultInfo { ArticleCollect = objArticleCollect };
                    objArticleResultInfoList.Add(objArticleResultInfo);
                }
                return objArticleResultInfoList;
            }

            List<ArticleInfoData> objList = null;
            ArticleInfoData objArticleInfoData = null;
            BizArticleInfoData objBizArticleInfoData = new BizArticleInfoData(objArticleCollectList.First().CategoryId.ToString());
            if (isLoadArticleContent && isLoadPictureCollection)
            {
                objList = objBizArticleInfoData.GetListCache("", TimeSpan.FromMinutes(1), string.Format("ArticleId in({0})", objArticleCollectList.Select(c => c.ArticleId).JoinComma()));
                if (objList.IsNoNull())
                {
                    List<ArticleInfoData> objArticleInfoDataList = null;
                    foreach (ArticleCollect objArticleCollect in objArticleCollectList)
                    {
                        objArticleResultInfo = new ArticleResultInfo
                        {
                            ArticleCollect = objArticleCollect
                        };
                        objArticleInfoDataList = objList.Where(c => c.ArticleId == objArticleCollect.ArticleId).ToList();
                        if (objArticleInfoDataList.IsNoNull())
                        {
                            objArticleInfoData = objArticleInfoDataList.FirstOrDefault(c => c.InfoDataType == ArticleInfoDataType.ArticleContent);
                            if (objArticleInfoData != null)
                            {
                                objArticleResultInfo.ArticleContent = objArticleInfoData.ArticleContent;
                            }
                            objArticleInfoData = objArticleInfoDataList.FirstOrDefault(c => c.InfoDataType == ArticleInfoDataType.PictureCollection);
                            if (objArticleInfoData != null)
                            {
                                objArticleResultInfo.PictureCollection = objArticleInfoData.ArticleContent.JsonDeserialize<List<ArticlePicture>>();
                            }
                        }
                        objArticleResultInfoList.Add(objArticleResultInfo);
                    }
                }
                return objArticleResultInfoList;
            }

            if (isLoadArticleContent)
            {
                objList = objBizArticleInfoData.GetListCache("", TimeSpan.FromMinutes(1), string.Format("ArticleId in({0}) AND InfoDataType=0", objArticleResultInfoList.Select(c => c.ArticleCollect.ArticleId).JoinComma()));
                if (objList.IsNoNull())
                {
                    foreach (ArticleCollect objArticleCollect in objArticleCollectList)
                    {
                        objArticleResultInfo = new ArticleResultInfo
                        {
                            ArticleCollect = objArticleCollect
                        };
                        objArticleInfoData = objList.FirstOrDefault(c => c.ArticleId == objArticleCollect.ArticleId);
                        if (objArticleInfoData != null)
                        {
                            objArticleResultInfo.ArticleContent = objArticleInfoData.ArticleContent;
                        }
                        objArticleResultInfoList.Add(objArticleResultInfo);
                    }
                }
            }
            if (isLoadPictureCollection)
            {
                objList = objBizArticleInfoData.GetListCache("", TimeSpan.FromMinutes(1), string.Format("ArticleId in({0}) AND InfoDataType=1", objArticleResultInfoList.Select(c => c.ArticleCollect.ArticleId).JoinComma()));
                if (objList.IsNoNull())
                {
                    foreach (ArticleCollect objArticleCollect in objArticleCollectList)
                    {
                        objArticleInfoData = objList.FirstOrDefault(c => c.ArticleId == objArticleResultInfo.ArticleCollect.ArticleId);
                        objArticleResultInfo = objArticleResultInfoList.FirstOrDefault(c => c.ArticleCollect.ArticleId == objArticleCollect.ArticleId);
                        if (objArticleResultInfo != null)
                        {
                            if (objArticleInfoData != null)
                            {
                                objArticleResultInfo.PictureCollection = objArticleInfoData.ArticleContent.JsonDeserialize<List<ArticlePicture>>();
                            }
                            continue;
                        }
                        objArticleResultInfo = new ArticleResultInfo
                        {
                            ArticleCollect = objArticleCollect
                        };
                        if (objArticleInfoData != null)
                        {
                            objArticleResultInfo.PictureCollection = objArticleInfoData.ArticleContent.JsonDeserialize<List<ArticlePicture>>();
                        }
                        objArticleResultInfoList.Add(objArticleResultInfo);
                    }
                }
            }
            return objArticleResultInfoList;
        }

        /// <summary>
        /// 将图片生成缩略图
        /// </summary>
        /// <param name="pictureUrl"></param>
        /// <returns></returns>
        public static string CreateThumbnailFromPicture(this string pictureUrl)
        {
            return pictureUrl;
        }

        /// <summary>
        /// 将内容里面的图片生成缩略图
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        public static string CreateThumbnailFromContent(this string content)
        {
            return content;
        }

        public static string ConvertTargetUrl(this ArticleInfo objArticleInfo, CategoryInfo objCategoryInfo = null, ArticlePlatformType objArticlePlatformType = ArticlePlatformType.Phone)
        {
            string targetUrl = null;
            if (objArticleInfo == null)
            {
                return targetUrl;
            }
            if (objCategoryInfo == null)
            {
                BizCategoryInfo objBizCategoryInfo = new BizCategoryInfo();
                objCategoryInfo = objBizCategoryInfo.GetRecordCache("", objArticleInfo.CategoryId, TimeSpan.FromMinutes(1));
            }
            if (objCategoryInfo != null)
            {
                objArticleInfo.TargetUrl = objArticleInfo.TargetUrl.IsNull() ? objCategoryInfo.TargetUrl : objArticleInfo.TargetUrl;
                objArticleInfo.PhoneTargetUrl = objArticleInfo.PhoneTargetUrl.IsNull() ? objCategoryInfo.PhoneTargetUrl : objArticleInfo.PhoneTargetUrl;
            }
            switch (objArticlePlatformType)
            {
                case ArticlePlatformType.PC:
                    targetUrl = objArticleInfo.TargetUrl.IsNull() ? "" : objArticleInfo.TargetUrl.Replace("{ArticleId}", objArticleInfo.ArticleId.ToString(), RegexOptions.IgnoreCase)
                     .Replace("{CategoryId}", objArticleInfo.CategoryId.ToString(), RegexOptions.IgnoreCase)
                     .Replace("{PublishDate}", objArticleInfo.PublishDate.ToString("yyyyMMdd"), RegexOptions.IgnoreCase); ;
                    break;
                case ArticlePlatformType.Phone:
                    targetUrl = objArticleInfo.PhoneTargetUrl.IsNull() ? "" : objArticleInfo.PhoneTargetUrl.Replace("{ArticleId}", objArticleInfo.ArticleId.ToString(), RegexOptions.IgnoreCase)
                      .Replace("{CategoryId}", objArticleInfo.CategoryId.ToString(), RegexOptions.IgnoreCase)
                      .Replace("{PublishDate}", objArticleInfo.PublishDate.ToString("yyyyMMdd"), RegexOptions.IgnoreCase);
                    break;
            }
            return targetUrl;
        }

        public static string ConvertTargetUrl(this ArticleCollect objArticleCollect, CategoryInfo objCategoryInfo = null, ArticlePlatformType objArticlePlatformType = ArticlePlatformType.Phone)
        {
            string targetUrl = null;
            if (objArticleCollect == null)
            {
                return targetUrl;
            }
            if (objCategoryInfo == null)
            {
                BizCategoryInfo objBizCategoryInfo = new BizCategoryInfo();
                objCategoryInfo = objBizCategoryInfo.GetRecordCache("", objArticleCollect.CategoryId, TimeSpan.FromMinutes(1));
            }
            if (objCategoryInfo != null)
            {
                objArticleCollect.TargetUrl = objArticleCollect.TargetUrl.IsNull() ? objCategoryInfo.TargetUrl : objArticleCollect.TargetUrl;
                objArticleCollect.PhoneTargetUrl = objArticleCollect.PhoneTargetUrl.IsNull() ? objCategoryInfo.PhoneTargetUrl : objArticleCollect.PhoneTargetUrl;
            }
            switch (objArticlePlatformType)
            {
                case ArticlePlatformType.PC:
                    targetUrl = objArticleCollect.TargetUrl.IsNull() ? "" : objArticleCollect.TargetUrl.Replace("{ArticleId}", objArticleCollect.ArticleId.ToString(), RegexOptions.IgnoreCase)
                     .Replace("{CategoryId}", objArticleCollect.CategoryId.ToString(), RegexOptions.IgnoreCase)
                     .Replace("{PublishDate}", objArticleCollect.PublishDate.ToString("yyyyMMdd"), RegexOptions.IgnoreCase); ;
                    break;
                case ArticlePlatformType.Phone:
                    targetUrl = objArticleCollect.PhoneTargetUrl.IsNull() ? "" : objArticleCollect.PhoneTargetUrl.Replace("{ArticleId}", objArticleCollect.ArticleId.ToString(), RegexOptions.IgnoreCase)
                      .Replace("{CategoryId}", objArticleCollect.CategoryId.ToString(), RegexOptions.IgnoreCase)
                      .Replace("{PublishDate}", objArticleCollect.PublishDate.ToString("yyyyMMdd"), RegexOptions.IgnoreCase);
                    break;
            }
            return targetUrl;
        }

        public static ArticleResultInfo ConvertTargetUrl(this ArticleResultInfo objArticleResultInfo, CategoryInfo objCategoryInfo = null, ArticlePlatformType objArticlePlatformType = ArticlePlatformType.Phone)
        {
            if (objArticleResultInfo == null)
            {
                return objArticleResultInfo;
            }
            BizCategoryInfo objBizCategoryInfo = new BizCategoryInfo();
            if (objArticleResultInfo.ArticleCollect != null)
            {
                if (objCategoryInfo == null)
                {
                    objCategoryInfo = objBizCategoryInfo.GetRecordCache("", objArticleResultInfo.ArticleCollect.CategoryId, TimeSpan.FromMinutes(1));
                }
                objArticleResultInfo.TargetUrl = objArticleResultInfo.ArticleInfo.ConvertTargetUrl(objCategoryInfo, objArticlePlatformType);
            }
            if (objArticleResultInfo.ArticleInfo != null)
            {
                if (objCategoryInfo == null)
                {
                    objCategoryInfo = objBizCategoryInfo.GetRecordCache("", objArticleResultInfo.ArticleInfo.CategoryId, TimeSpan.FromMinutes(1));
                }
                objArticleResultInfo.TargetUrl = objArticleResultInfo.ArticleInfo.ConvertTargetUrl(objCategoryInfo, objArticlePlatformType);
            }
            return objArticleResultInfo;
        }

        public static List<ArticleResultInfo> ConvertTargetUrl(this List<ArticleResultInfo> objArticleResultInfoList, CategoryInfo objCategoryInfo = null, ArticlePlatformType objArticlePlatformType = ArticlePlatformType.Phone)
        {
            if (objArticleResultInfoList.IsNull())
            {
                return objArticleResultInfoList;
            }
            if (objCategoryInfo != null)
            {
                foreach (ArticleResultInfo objArticleResultInfo in objArticleResultInfoList)
                {
                    objArticleResultInfo.ConvertTargetUrl(objCategoryInfo, objArticlePlatformType);
                }
                return objArticleResultInfoList;
            }

            List<int> categoryIdList = new List<int>();
            foreach (ArticleResultInfo objArticleResultInfo in objArticleResultInfoList)
            {
                if (objArticleResultInfo.ArticleCollect != null)
                {
                    categoryIdList.Add(objArticleResultInfo.ArticleCollect.CategoryId);
                }
                if (objArticleResultInfo.ArticleInfo != null)
                {
                    categoryIdList.Add(objArticleResultInfo.ArticleInfo.CategoryId);
                }
            }
            categoryIdList = categoryIdList.Distinct().ToList();
            BizCategoryInfo objBizCategoryInfo = new BizCategoryInfo();
            List<CategoryInfo> objCategoryInfoList = objBizCategoryInfo.GetListInCache("", TimeSpan.FromMinutes(1), categoryIdList);
            if (objCategoryInfoList.IsNull())
            {
                return objArticleResultInfoList;
            }

            foreach (ArticleResultInfo objArticleResultInfo in objArticleResultInfoList)
            {
                objCategoryInfo = objCategoryInfoList.FirstOrDefault();
                objArticleResultInfo.ConvertTargetUrl(objCategoryInfo);
            }
            return objArticleResultInfoList;
        }
    }
}
